<?php
App::uses('AppController', 'Controller');

/**
 * Categories Controller
 *
 * @property Category $Category
 */
class CategoriesController extends AppController {
	
	public function beforeFilter() {
		parent::beforeFilter();
	}
	
	public function admin_home() {	
	}
	public function admin_index() {
		$parents = $this->Category->find('all', array(
			'fields' => array('Category.id', 'Category.name'),
			'conditions' => array('Category.parent_id' => 0),
			'order' => array('Category.name' => 'ASC')
		));
		$this->set('parents', $parents);		
	}
	public function admin_view() {	
	}
	
	public function admin_edit($id = null) {
		$this->Category->id = $id;
		if (!$this->Category->exists()) {
			throw new NotFoundException('Invalid Category');
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Category->save($this->request->data)) {
				$this->Session->setFlash('The Category has been saved');
				$url = $this->Session->read('Controller.toUrl');
				$this->redirect($url);
			} else {
				$this->Session->setFlash('The Category could not be saved. Please, try again.');
			}
		} else {
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
			$this->request->data = $this->Category->find('first', $options);
			
			$parents = $this->Category->find('all', array(
				'fields' => array('Category.id', 'Category.name'),
				'conditions' => array('Category.parent_id' => 0),
				'order' => array('Category.name' => 'ASC')
			));
			$this->set('parents', $parents);
		}
	}	

	public function ajax_index(){
		$this->paginate = array(
			'recursive' => -1,
			'fields' => array('Category.id', 'Category.parent_id', 'Category.name', 'Category.status', 'Category.created', 'Category.modified', 'Parent.name'),
			'limit' => 10,
			'order' => array('Category.parent_id' => 'ASC', 'Category.name' => 'ASC'),
			'joins' => array(
				array('table' => 'categories',
					'alias' => 'Parent',
					'type'	=> 'LEFT',
					'conditions' => array('Category.parent_id = Parent.id')
				)
			)		
		);
		$this->set('categories', $this->paginate());
	}
	
	public function api_add() {
		$error = -1;
		if ($this->request->is('post')) {
			$this->Category->create();
			if ($this->Category->save($this->request->data)) {
				$error = 0;
				$message = 'The Category has been saved';
			} else {
				$error = 1;
				$message = 'The Category could not be saved. Please, try again.';
			}
		}
		$this->set('error', $error);
		$this->set('data', $message);
	}
	
	public function api_delete($id = null) {
		$error = -1;
		$this->Category->id = $id;
		if (!$this->Category->exists()) {
			$message = 'Invalid Category';
		}
		if ($this->Category->delete()) {
			$error = 0;
			$message = 'Category deleted';
		}
		else{
			$error = 1;
			$message = 'Category was not deleted';			
		}
		$this->set('error', $error);
		$this->set('data', $message);
	}
	
	public function api_status($id = null){
		$error = -1;
		$this->Category->id = $id;
		if (!$this->Category->exists()) {
			$message = 'Invalid Category';
		}
		else{		
			if($this->Category->updateAll(
			array('Category.status' => '1 - Category.status', 'modified' => 'NOW()'),
			array('Category.id' => $id))){
				$error = 0;
				$message = 'Status has been changed';
			}
			else{
				$error = 1;
				$message = 'Status could not be changed';					
			}
		}
		$this->set('error', $error);
		$this->set('data', $message);		
	}
}
?>